package com.apes.fn.server.workDesk.smartHelper.service;

import com.apes.fn.server.workDesk.smartHelper.process.CommonSmartDataProcess;
import com.apes.fn.server.workDesk.smartHelper.process.Finance.FinanceProcess;
import com.apes.fn.server.workDesk.smartHelper.process.inStore.InStoreCarProcess;
import com.apes.fn.server.workDesk.smartHelper.process.inStore.InStoreProcess;
import com.apes.fn.server.workDesk.smartHelper.process.inStoreBefore.InStoreBeforeProcess;
import com.apes.fn.server.workDesk.smartHelper.process.outStore.OutStoreProcess;
import com.apes.fn.server.workDesk.smartHelper.process.person.PersonProcess;
import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.util.Tools;
import com.apes.scm.masterdata.frame.repository.DeptRepository;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StopWatch;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * @program: apes-server
 * @description:
 * @author: czs
 * @create: 2023-06-14 17:12
 **/
@Service("smartHelperService")
public class SmartHelperService extends DomainService {

    @Autowired
    private MongoTemplate mongoTemplate;

    private Object findSmartHelper(SimpleRequest request){
//        StopWatch sw = new StopWatch();
//        sw.start();
        List<CommonSmartDataProcess> processList =new ArrayList<>();
        JSONObject jo = request.getJO();
        if (jo.get("DEPT_ID")!=null){
            jo.put("DEPT_NAME", SpringManager.getBean(DeptRepository.class).findOne(jo.getString("DEPT_ID")).getName());
        }else {
            jo.remove("DEPT_ID");
        }
        processList.add(new InStoreBeforeProcess());
        processList.add(new InStoreProcess());
        processList.add(new OutStoreProcess());
        processList.add(new FinanceProcess());
        processList.add(new PersonProcess());
        List<Map> collect = new ArrayList<>();
        try {
            collect = processList.parallelStream().map(commonSmartDataProcess -> commonSmartDataProcess.process(jo)).filter(Objects::nonNull).collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
            if (!"请检查连接、参数是否正确".contains(e.getMessage())){
                Document document = new Document();
                String firstStackTrace = Tools.getAllStackTrace(e);
                Assert.notNull(firstStackTrace,e.getMessage());
//                String s = firstStackTrace.getClassName() +":"+firstStackTrace.getMethodName() +":"+firstStackTrace.getLineNumber();
                document.put("DEPT_ID", jo.get("DEPT_ID"));
                document.put("data", jo);
                document.put("session", request.getSession().getSessionId());
                document.put("post", request.getSession().getPostId());
                document.put("user", request.getSession().getUserId());
                document.put("errInfo",e.getMessage());
                document.put("stackTrace",firstStackTrace);
                document.put("method","findSmartHelper");
                mongoTemplate.getCollection("smartHelperError").insertOne(document);
            }
            throw new RuntimeException("查询智能店助有误:"+e.getMessage());
        }
//        CommonSmartDataProcess commonSmartDataProcess = new CommonSmartDataProcess();
//        Map process = commonSmartDataProcess.process(request.getJO());
        List<Map> weightValue = collect.stream()
                .sorted(Comparator.comparing(o -> ((Integer) o.get("weightValue"))))
                .collect(Collectors.toList());
//        sw.stop();
//        System.out.println("智能店助总耗时:"+sw.getLastTaskTimeMillis());
        return weightValue;
    }

    private Object findInStoreCarData(SimpleRequest request){
        JSONObject jo = request.getJO();
        CommonSmartDataProcess inStoreCarProcess = new InStoreCarProcess();
        Map hashMap = inStoreCarProcess.process(jo);
        return hashMap;
    }
}
